package com.digital.library.modules.system.controller;

import com.digital.library.common.annotation.Log;
import com.digital.library.common.annotation.Permission;
import com.digital.library.common.vo.Result;
import com.digital.library.modules.system.domain.LsBook;
import com.digital.library.modules.system.service.LsBookService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 图书控制类
 */
@RestController
@RequestMapping("/book")
public class LsBookController {

    @Autowired
    LsBookService lsBookService;

    @PostMapping(value = "/add")
    @Log(value = "新增图书", operateType = 2)
    @Permission(permission = "supermanager")
    @ApiOperation(value="新增图片")
    public Result<?> addBook(@RequestBody LsBook lsBook){
        lsBook.setCreateBy("admin");
        lsBook.setCreateTime(new Date());
        lsBookService.save(lsBook);

        return Result.ok(lsBook);
    }

    @PostMapping(value = "/query")
    @Log(value = "查询图书", operateType = 1)
    @ApiOperation(value="查询图书")
    public Result<?> queryBook(@RequestBody LsBook lsBook){
        List<LsBook> vo = lsBookService.list(lsBook);

        return Result.ok(vo);
    }

    @PostMapping(value = "/update")
    @Log(value = "更新图书", operateType = 3)
    @Permission(permission = "supermanager")
    @ApiOperation(value="更新图书")
    public Result<?> updateBook(@RequestBody LsBook lsBook){
        lsBook.setUpdateBy("admin");
        lsBook.setUpdateTime(new Date());
        int res = lsBookService.update(lsBook);
        String mes = res>0?"更新图书成功！":"未找到相关图书信息！";

        return Result.ok(mes);
    }

    @GetMapping(value = "/delete")
    @Log(value = "删除图书", operateType = 2)
    @Permission(permission = "supermanager")
    @ApiOperation(value="删除图书")
    public Result<?> deleteBook(@RequestParam(name = "id") int id){
        int res = lsBookService.deleteById(id);
        String mes = res>0?"删除图书成功！":"未找到相关图书信息！";

        return Result.ok(mes);
    }



}
